package com.study.samsung.Control;

import com.study.samsung.Entity.User;
import com.study.samsung.Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/enroll")
public class EnrollServlet extends HttpServlet {
    UserService us = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.getRequestDispatcher("/WEB-INF/jsp/enroll.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        System.out.println("wolaile");
        PrintWriter writer = resp.getWriter();
        String nickname = req.getParameter("nickname");
        String uname = req.getParameter("uname");
        String sex = req.getParameter("sex");
        String uphone = req.getParameter("uphone");
        String birth = req.getParameter("birth");
        String account = req.getParameter("account");
        String upwd = req.getParameter("upwd");
        User u = us.dophone(uphone);
        if (u==null){
            Integer add = us.enroll(nickname,uname,sex,uphone,birth,account,upwd);
            writer.println(add);
        }else {
            writer.println(-1);
        }
        writer.close();
    }
}
